﻿Public Class OvertimeAdd
    Dim empid As Int16
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lbl24h.Text = TimeOfDay.ToString("HH:mm:ss")
    End Sub

    Private Sub loadovertime()
        Dim dt As New DataTable
        cmd.Parameters("@id") = empid
        cmd.Parameters("@date") = Now.Date
        dt = cmd.ExecuteDataTable("SELECT ot_in,ot_out FROM overtime_tbl WHERE emp_id=@id AND ot_date=@date")
        lblin.Text = dt.Rows(0).Item(0).ToString
        lblout.Text = dt.Rows(0).Item(1).ToString
    End Sub
    Private Sub btnin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnin.Click

        If lbl24h.Text < "17:00:00" Then
            MessageBox.Show("Please wait untill 5:00 PM (17:00:00)")
            Exit Sub
        End If

        If lbl24h.Text = "" Then
            MessageBox.Show("សូមរង់ចាំបន្តិច")
            Exit Sub
        End If
        cmd.Parameters("@id") = empid
        cmd.Parameters("@in") = lbl24h.Text
        cmd.Parameters("@date") = Now.Date
        'Try
        '    cmd.ExecuteNonQuery("UPDATE overtime_tbl set ot_in=@in WHERE ot_date=@date AND emp_id=4")
        'Catch ex As Exception
        '    MessageBox.Show(ex.ToString)
        'End Try

        cmd.ExecuteNonQuery("UPDATE overtime_tbl set ot_in=@in WHERE ot_date=@date AND emp_id=@id")
        MessageBox.Show("Add Sucess")
        btnin.Enabled = False
        OvertimeAdd_Load(sender, e)
        btnout.Enabled = True

    End Sub

    Private Sub OvertimeAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        empid = frmworktime.emp_id
        loadovertime()

        
        If lblin.Text <> "" Then
            btnin.Enabled = False
        Else
            btnout.Enabled = False
        End If


        If lblout.Text <> "" Then
            btnout.Enabled = False
        End If

    End Sub

    Private Sub btnout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnout.Click
        If lbl24h.Text = "" Then
            MessageBox.Show("សូមរង់ចាំបន្តិច")
            Exit Sub
        End If
        cmd.Parameters("@id") = empid
        cmd.Parameters("@out") = lbl24h.Text
        cmd.Parameters("@date") = Now.Date

        cmd.ExecuteNonQuery("UPDATE overtime_tbl set ot_out=@out WHERE ot_date=@date AND emp_id=@id")
        MessageBox.Show("Add Sucess")
        btnout.Enabled = False
        OvertimeAdd_Load(sender, e)

    End Sub
End Class